package com.co.user.service.impl;

import com.co.common.util.IdWorker;
import com.co.user.pojo.History;
import com.co.user.pojo.Like;
import com.co.user.mapper.LikeMapper;
import com.co.user.service.LikeService;
import com.co.video.feign.DetailFeign;
import com.co.video.pojo.Detail;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * UsLike业务层接口实现类
 */
@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private DetailFeign detailFeign;

    @Autowired
    private IdWorker idWorker;

    /**
     * UsLike条件+分页查询
     *
     * @param like 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Like> findPage(Like like, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(like);
        //执行搜索
        return new PageInfo<Like>(likeMapper.selectByExample(example));
    }

    /**
     * UsLike分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Like> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Like>(likeMapper.selectAll());
    }

    /**
     * UsLike条件查询
     *
     * @param like
     * @return
     */
    @Override
    public List<Like> findList(Like like) {
        //构建查询条件
        Example example = createExample(like);
        //根据构建的条件查询数据
        return likeMapper.selectByExample(example);
    }


    /**
     * UsLike构建查询对象
     *
     * @param like
     * @return
     */
    public Example createExample(Like like) {
        Example example = new Example(Like.class);
        Example.Criteria criteria = example.createCriteria();
        if (like != null) {
            // 主键
            if (!StringUtils.isEmpty(like.getId())) {
                criteria.andEqualTo("id", like.getId());
            }
            // 用户表主键
            if (!StringUtils.isEmpty(like.getUsername())) {
                criteria.andLike("username", "%" + like.getUsername() + "%");
            }
            // vdetail的主键
            if (!StringUtils.isEmpty(like.getAid())) {
                criteria.andEqualTo("aid", like.getAid());
            }
            // 创建时间
            if (!StringUtils.isEmpty(like.getCreateTime())) {
                criteria.andEqualTo("createTime", like.getCreateTime());
            }
            // 修改时间
            if (!StringUtils.isEmpty(like.getUpdateTime())) {
                criteria.andEqualTo("updateTime", like.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        likeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 清空个人喜爱
     */
    @Override
    public void deleteAll() {
        likeMapper.deleteAll();
    }

    /**
     * 修改UsLike
     *
     * @param like
     */
    @Override
    public void update(Like like) {
        likeMapper.updateByPrimaryKey(like);
    }

    /**
     * 增加UsLike
     *
     * @param like
     */
    @Override
    public void add(Like like) {

        //获取用户名
        String username = like.getUsername();
        //获取视频的aid
        String aid = like.getAid();

        Like likeData = likeMapper.selectOne(like);

        if(likeData==null){
            like.setCreateTime(new Date());
            like.setId(String.valueOf(idWorker.nextId()));
            likeMapper.insertSelective(like);
            return;
        }
        if (aid.equals(likeData.getAid())){
            like.setCreateTime(new Date());
            like.setId(likeData.getId());
        }
        likeMapper.updateByPrimaryKey(like);

        }

    /**
     * 根据ID查询UsLike
     *
     * @param id
     * @return
     */
    @Override
    public Like findById(String id) {
        return likeMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询UsLike全部数据
     *
     * @return
     */
    @Override
    public List<Like> findAll() {
        return likeMapper.selectAll();
    }

    /**
     * 根据用户名查询喜爱列表
     * @param username
     * @return
     */
    @Override
    public List<Detail> findLikeByUsername(String username) {



        List<Detail> likeVideoList = new ArrayList<>();

        History history = new History();
        history.setUsername(username);

        //获取like中的aid并以最近优先排序
        List<String> aidList = likeMapper.findLikeByUsername(username);
        for (String aid : aidList) {

            //通过aid查询视频对象
            Detail detail = detailFeign.findById(aid).getData();

            likeVideoList.add(detail);
        }

        return likeVideoList;
    }
}
